package patrol.mobile.com.mobilepatrol.persenter;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import patrol.mobile.com.mobilepatrol.api.ByteCallback;
import patrol.mobile.com.mobilepatrol.bean.PatrolDetailInfoBean;
import patrol.mobile.com.mobilepatrol.manager.TrackManager;
import patrol.mobile.com.mobilepatrol.utils.Compress;
import patrol.mobile.com.mobilepatrol.utils.Constants;
import patrol.mobile.com.mobilepatrol.utils.SPUtils;
import patrol.mobile.com.mobilepatrol.view.BridgeUpView;
import patrol.mobile.com.mobilepatrol.view.UploadImgView;

/**
 * Created by apple on 17/9/30.
 */

public class TrackUpPer {

    private BridgeUpView mView;
    private UploadImgView imgView;
    private String json;
    private String patrolId;//巡道主表的id

    public TrackUpPer(BridgeUpView mView,UploadImgView  imgView ,String json,String patrolId) {
        this.mView = mView;
        this.imgView = imgView;
        this.json = json;
        this.patrolId=patrolId;
    }

    public void up2(String url,String type) {
        OkHttpUtils
                .post()
                .addHeader("Content-RouteType","application/json; charset=utf-8")
                .url(SPUtils.get(Constants.current_ser,"") + url)
                .addParams("type",type)
                .addParams("json",json)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        mView.onError("上传失败");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            JSONObject obj = new JSONObject(response);
                            int state = obj.optInt("code");
                            if (state ==200){
                                mView.onSuccess("上传成功");
                            }else{
                                mView.onError("上传失败");
                            }
                        } catch (JSONException e) {

                        }

                    }
                });

    }
    public void up(String url,String type) {
        OkHttpUtils
                .post()
                .addHeader("Content-RouteType","charset=utf-8")
                .url(SPUtils.get(Constants.current_ser,"") + url)
                .addParams("type",type)
                .addParams("json",json)
                .build()
                .execute(new ByteCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        mView.hideLoading();
                        mView.onError(e.getMessage());
                    }

                    @Override
                    public void onResponse(byte[] response, int id) {
                        String result = null;
                        try {
                            result = Compress.decompressByteArray(response);
                            JSONObject object = new JSONObject(result);
                            mView.hideLoading();
                            if (object.getInt("code")==200){
                                mView.onSuccess("上传实体成功，上传图片开始");
                                uploadImg(patrolId);
                            }else{
                                mView.onError("上传失败");
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });

    }
    private void uploadImg(String id) {
        List<PatrolDetailInfoBean> detailInfos = TrackManager.getAllDetailList(id);
        for (int i=0;i<detailInfos.size();i++){
            PatrolDetailInfoBean d = detailInfos.get(i);
            String ID = d.ID;
            String pn = d.photoNumber;
            if (pn!=null&&!pn.equals("")){
                String[] imgs = pn.split(",");
                uploadImg(ID,imgs);
            }

        }
    }
    /**
     * 一个明细的多张图片
     */
    private void uploadImg(String id,String[] imgs) {
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .addFormDataPart("bizID", id);

        //多张图片
        for (int i = 0; i < imgs.length; i++) {
            File file = new File(imgs[i]);//filePath 图片地址
            RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            builder.addFormDataPart("file", imgs[i], imageBody);//"imgfile"+i 后台接收图片流的参数名
        }

        List<MultipartBody.Part> parts = builder.build().parts();
        UploadPicsPresenter picsPresenter = new UploadPicsPresenter(imgView,parts);
        picsPresenter.uploadTrackImg();

    }
}
